package com.ljy.my_study.ut;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author lijunying
 * @date 2020/7/27 13:37
 */
public class Http2MarkdownConverter {

    public static void main(String[] args) {
        String str="<table>\n" +
                "  <thead class=\"ant-table-thead\">\n" +
                "    <tr>\n" +
                "      <th key=name>名称</th>\n" +
                "      <th key=type>类型</th>\n" +
                "      <th key=required>是否必须</th>\n" +
                "      <th key=default>默认值</th>\n" +
                "      <th key=desc>备注</th>\n" +
                "      <th key=sub>其他信息</th>\n" +
                "    </tr>\n" +
                "  </thead>\n" +
                "  <tbody className=\"ant-table-tbody\">\n" +
                "     <tr key=0-0>\n" +
                "       <td key=0>\n" +
                "         <span style=\"padding-left: 0px\">\n" +
                "           <span style=\"color: #8c8a8a\"></span> \n" +
                "              msg\n" +
                "         </span>\n" +
                "       </td>\n" +
                "       <td key=1>\n" +
                "          <span>string</span>\n" +
                "       </td>\n" +
                "       <td key=2>必须</td>\n" +
                "       <td key=3></td>\n" +
                "       <td key=4>\n" +
                "          <span style=\"white-space: pre-wrap\">响应信息</span>\n" +
                "       </td>\n" +
                "       <td key=5></td>\n" +
                "     </tr>\n" +
                "    <tr key=0-1>\n" +
                "       <td key=0>\n" +
                "          <span style=\"padding-left: 0px\">\n" +
                "             <span style=\"color: #8c8a8a\"></span>\n" +
                "              code\n" +
                "          </span>\n" +
                "       </td>\n" +
                "       <td key=1><span>integer</span></td><td key=2>必须</td><td key=3></td><td key=4><span style=\"white-space: pre-wrap\">响应状态码</span></td><td key=5><p key=2><span style=\"font-weight: '700'\">format: </span><span>int32</span></p></td></tr><tr key=0-2><td key=0><span style=\"padding-left: 0px\"><span style=\"color: #8c8a8a\"></span> data</span></td><td key=1><span>object</span></td><td key=2>非必须</td><td key=3></td><td key=4><span style=\"white-space: pre-wrap\">响应数据</span></td><td key=5></td></tr>\n" +
                "               </tbody>\n" +
                "              </table>";
        new Http2MarkdownConverter().converte(str);
    }

    public void converte(String str){
        String startStr="<table>";
        String endStr="</table>";
        int start =str.indexOf(startStr);
        int end=str.indexOf(endStr);
        String tableStr=str.substring(start,end+endStr.length());
        String headStartStr="<thead";
        String headEndStr="</thead>";
        int headStartIndex=tableStr.indexOf(headStartStr);
        int headEndIndex=tableStr.indexOf(headEndStr);
        String headStr=tableStr.substring(headStartIndex,headEndIndex);
        Set<String> headSet=new HashSet<>();
        String thStartStr="<th ";
        String thEndStr="</th>";
        int thStartIndex=0;
        int thEndIndex=0;
        do{
            thStartIndex=headStr.indexOf(thStartStr);
            if(thStartIndex==-1){
                break;
            }
            while(headStr.charAt(thStartIndex)!='>'){
                thStartIndex++;
            }
            thEndIndex=thStartIndex;
            while(headStr.charAt(thEndIndex)!='<'){
                thEndIndex++;
            }
            String value=headStr.substring(thStartIndex+1,thEndIndex).trim();
            headSet.add(value);
            headStr=headStr.substring(thEndIndex);
        }while(true);
        List<List<String>> listList=new ArrayList<>();

        String bodyStartStr="<tbody";
        String bodyEndStr="</tbody>";
        int bodyStartIndex=tableStr.indexOf(bodyStartStr);
        int bodyEndIndex=tableStr.indexOf(bodyEndStr);
        String bodyStr=tableStr.substring(bodyStartIndex,bodyEndIndex);
        String tdStartStr="<td key=";
        String tdEndStr="</td>";
        int tdStartIndex=0;
        int tdEndIndex=0;
        do{
            List<String> list=new ArrayList<>();

            tdStartIndex=bodyStr.indexOf(tdStartStr);
            if(tdStartIndex==-1){
                break;
            }
            tdEndIndex=bodyStr.indexOf(tdEndStr);
            String tdStr=bodyStr.substring(tdStartIndex,tdEndIndex);
            boolean isAdd=false;
            while(true){

//                int startIndex=tdStr.indexOf(">");
//                if(startIndex==-1){
//                    break;
//                }
                int endIndex=tdStr.indexOf("</");
                if(endIndex==-1){
                    break;
                }
                int startIndex=endIndex;
                while(tdStr.charAt(startIndex)!='>'){
                    startIndex--;
                }
                String valueStr=tdStr.substring(startIndex+1,endIndex);
                if(!valueStr.contains("<")&&!valueStr.contains("</")&&!valueStr.contains("\n")&&!"".equals(valueStr)){
                    list.add(valueStr);
                    isAdd=true;

                }
                tdStr=tdStr.substring(endIndex);
            }
            listList.add(list);
            bodyStr=bodyStr.substring(tdEndIndex);
        }while(true);

        System.out.println(listList);
    }
}
